#include "stdafx.h"
#include "GameInputWnd.h"
#include "RectStatic.h"
#include "RectSelect.h"
#include "PFMEngine.h"
#include "IniRead.h"
#include "InputState.h"
#include "InputGage.h"

GameInputWnd::GameInputWnd()
:m_pRectBackGround(NULL)
,m_pGameScene(NULL)
,m_nCurState(0)
{
	m_pState[STATE_WAITING] = new InputStateWaiting;
	m_pState[STATE_GREETING] = new InputStateGreeting;
	m_pState[STATE_SELECTPRODUCT] = new InputStateSelectProduct;
	m_pState[STATE_SENDINGCHANGE] = new InputStateSendChange;
	m_pState[STATE_FAREWELL] = new InputStateFarewell;
}

GameInputWnd::~GameInputWnd()
{
	for(int i = 0; i < STATE_MAX; ++i)
	{
		SAFE_DELETE(m_pState[i]);
	}

	Release();
}

void GameInputWnd::Release()
{
	SAFE_DELETE(m_pRectBackGround);		
	SAFE_DELETE(m_pInputGage);
	m_nCurState = STATE_WAITING;
}

void GameInputWnd::Update()
{
	m_pRectBackGround->Update();

	if( m_pState[m_nCurState]->IsNextSet() )
	{
		m_pState[m_nCurState]->OnExit();
		m_nCurState = m_pState[m_nCurState]->GetNextState();
		m_pState[m_nCurState]->OnEnter();
	}
	m_pState[m_nCurState]->OnUpdate();
}


void GameInputWnd::SetNextState( int state )
{
	m_pState[m_nCurState]->SetNextState( state );
}


void GameInputWnd::Init(int nStageIndex, GameScene* pGameScene)
{
	Release();

	m_pGameScene = pGameScene;

	int nPosX, nPosY, nWidth, nHeight;
	char szResourceName[MAX_PATH] = {0,};
	INI_TYPE eIniIndex = (INI_TYPE)(INI_TYPE_STAGE_1 + nStageIndex);

	m_pRectBackGround = new RectStatic;
	nPosX   = GetIniReader().GetIniKeyValue_Int(eIniIndex , "INPUT_RESOURCE", "BackGround_PosX");
	nPosY   = GetIniReader().GetIniKeyValue_Int(eIniIndex , "INPUT_RESOURCE", "BackGround_PosY");
	nWidth  = GetIniReader().GetIniKeyValue_Int(eIniIndex , "INPUT_RESOURCE", "BackGround_Width");
	nHeight = GetIniReader().GetIniKeyValue_Int(eIniIndex , "INPUT_RESOURCE", "BackGround_Height");	
	GetIniReader().GetIniKeyValue_String(eIniIndex , szResourceName, MAX_PATH, "INPUT_RESOURCE", "BackGround_Img");
	m_pRectBackGround->Create(GetPFMEngine().CreateRect(szResourceName), nPosX, nPosY, nWidth, nHeight);

	m_rcSize.left = nPosX;
	m_rcSize.top = nPosY;
	m_rcSize.right = nPosX + nWidth;
	m_rcSize.bottom = nPosY + nHeight;

	m_pInputGage = new InputGage;
	nPosX   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "INPUT_RESOURCE", "Timer_PosX");
	nPosY   = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "INPUT_RESOURCE", "Timer_PosY");
	int nMargin = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON , "INPUT_RESOURCE", "Timer_Margin");
	m_pInputGage->Init(nPosX, nPosY, nMargin);

	m_nCurState = STATE_WAITING;
	for(int i = 0; i < STATE_MAX; ++i)
	{
		m_pState[i]->Init(nStageIndex, pGameScene, m_pInputGage);
	}	
}

void GameInputWnd::OnMouseMove(int nPosX, int nPosY)
{
	m_pState[m_nCurState]->OnMouseMove(nPosX, nPosY);
}

void GameInputWnd::OnMouseLDown(int nPosX, int nPosY)
{
	m_pState[m_nCurState]->OnMouseLDown(nPosX, nPosY);
}

void GameInputWnd::OnMouseLUp(int nPosX, int nPosY)
{
	m_pState[m_nCurState]->OnMouseLUp(nPosX, nPosY);
}